﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 控制主角球吞噬

public class Eater : MonoBehaviour
{
    public Text scoreBoard;
    public GameObject levelBoard;
    public float levelShowTime;

    private int score;
    private uint level;
    private float timer;
    private BallMovement ballMovement;

    // Start is called before the first frame update
    void Start()
    {
        score = 0;
        level = 1;
        timer = 0.0f;
        ballMovement = GetComponent<BallMovement>();
        ScoreUpdate();
    }

    // Update is called once per frame
    void Update()
    {
        if (levelBoard.activeInHierarchy)
        {
            timer -= Time.deltaTime;
            if (timer <= 0.0f)
            {
                levelBoard.SetActive(false);
            }
        }
    }

    void OnTriggerEnter(Collider collider)
    {
        if (collider.tag == "Coin")
        {
            score++;
            Destroy(collider.gameObject);
            ScoreUpdate();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "FleeBall")
        {
            score += 5;
            Destroy(collision.collider.gameObject);
            ScoreUpdate();
        }
    }

    void ScoreUpdate()
    {
        string scoreStr = "得分：";
        scoreBoard.text = scoreStr + score.ToString();

        if (level < 6 && Mathf.Pow(2, level) * 5 <= score)
        {
            level++;

            ballMovement.speed -= 1f;
            float targetScale = transform.localScale.y + 1;
            transform.position = new Vector3(transform.position.x, targetScale / 2, transform.position.z);
            transform.localScale = new Vector3(targetScale, targetScale, targetScale);

            timer = levelShowTime;
            levelBoard.SetActive(true);
        }
    }
}
